package cn.jingyuan.swan.gw.web.config;

import cn.jingyuan.swan.cloud.web.DefaultWebMvcConfiguration;
import cn.jingyuan.swan.gw.locator.ResourceLocator;
import cn.jingyuan.swan.gw.manager.AccessCheckManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;

import javax.annotation.Resource;
import java.util.Collections;

@Slf4j
@Configuration
public class AppWebConfig {

    @Resource
    ResourceLocator resourceLocator;

    @Bean
    public DefaultWebMvcConfiguration webMvcConfig() {
        return new DefaultWebMvcConfiguration();
    }

    @Bean
    public OAuth2WebSecurityExpressionHandler securityExpressionHandler(ApplicationContext applicationContext) {
        OAuth2WebSecurityExpressionHandler expressionHandler = new OAuth2WebSecurityExpressionHandler();
        expressionHandler.setApplicationContext(applicationContext);
        return expressionHandler;
    }

    @Bean
    public AccessCheckManager accessCheckManager() {
        AccessCheckManager manager = new AccessCheckManager(resourceLocator);
        manager.addIgnoreAuthorities(Collections.emptyList());
        return manager;
    }

}
